#include "MainUI.h"


MainUI::MainUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainUI)
{
    ui->setupUi(this);
    
    m_initData();
    m_initUI();
    m_initTimer();

}

MainUI::~MainUI()
{
    delete ui;
}

void MainUI::m_initData()
{
    /*波形是否左右自动滑动*/
    m_isOpenSlip = true;
    
    /*清空串口接收总BUF*/
    m_SerialRevBuf.clear();
    memset(analysisBuf,0,sizeof(analysisBuf));
    analysisIndex = 0;
    m_crcErrCnt = 0 ;
    m_crcPassCnt = 0 ;
    m_revPtCnt = 0 ;
    
    /*信号与槽集合*/
    connect(&m_serial,SIGNAL(readyRead()),this,SLOT(on_serialReadyRead()));
}

void MainUI::m_initUI()
{
    m_intiChart();
    m_initLblVerse();
    m_refreshSerialList();
    m_initLcd();
    
    showMaximized();
}

void MainUI::m_initLcd()
{
    /*设置数字字体：Outline,轮廓突出，颜色为背景色；Filled，内部填充型，颜色为黑色；Flat,平面型*/
    QPalette lcdpat;
    
    ui->lcdNumberCrcPassCnt->setSegmentStyle(QLCDNumber::Flat);
    ui->lcdNumberErrCnt->setSegmentStyle(QLCDNumber::Flat);

    lcdpat = ui->lcdNumberCrcPassCnt->palette();
    lcdpat.setColor(QPalette::Normal,QPalette::WindowText,Qt::black);
    ui->lcdNumberCrcPassCnt->setPalette(lcdpat);
    
    lcdpat = ui->lcdNumberErrCnt->palette();
    lcdpat.setColor(QPalette::Normal,QPalette::WindowText,Qt::red);
    ui->lcdNumberErrCnt->setPalette(lcdpat);
}


void MainUI::on_btnZoomIn_clicked()
{
    m_chart->zoomIn();
}

void MainUI::on_btnZoomOut_clicked()
{
    m_chart->zoomOut();
}

void MainUI::on_btnUp_clicked()
{
#define SERIE_TICK_Y_UP  (-SERIE_TICK_Y) /*轴向上平移 则图形相对向下，所有有负号*/
    
    QValueAxis *axisY = dynamic_cast<QValueAxis*>(m_chart->axisY());//
    qreal cur_y_min = axisY->min();
    qreal cur_y_max = axisY->max();
    axisY->setRange(cur_y_min + SERIE_TICK_Y_UP, cur_y_max + SERIE_TICK_Y_UP);
}

void MainUI::on_btnDown_clicked()
{
#define SERIE_TICK_Y_DOWM  (+SERIE_TICK_Y) /*轴向上平移 则图形相对向下，所有有负号*/
    
    QValueAxis *axisY = dynamic_cast<QValueAxis*>(m_chart->axisY());//
    qreal cur_y_min = axisY->min();
    qreal cur_y_max = axisY->max();
    axisY->setRange(cur_y_min + SERIE_TICK_Y_DOWM, cur_y_max + SERIE_TICK_Y_DOWM);
}

void MainUI::on_btnLeft_clicked()
{
#define SERIE_TICK_X_LEFT  (SERIE_TICK_X) /*轴向上平移 则图形相对向下，所有有负号*/
    
    QValueAxis *axisX = dynamic_cast<QValueAxis*>(m_chart->axisX());//
    qreal cur_x_min = axisX->min();
    qreal cur_x_max = axisX->max();
    axisX->setRange(cur_x_min + SERIE_TICK_X_LEFT, cur_x_max + SERIE_TICK_X_LEFT);
}

void MainUI::on_btnRight_clicked()
{
#define SERIE_TICK_X_RIGHT  (-SERIE_TICK_X) /*轴向上平移 则图形相对向下，所有有负号*/
    
    QValueAxis *axisX = dynamic_cast<QValueAxis*>(m_chart->axisX());//
    qreal cur_x_min = axisX->min();
    qreal cur_x_max = axisX->max();
    axisX->setRange(cur_x_min + SERIE_TICK_X_RIGHT, cur_x_max + SERIE_TICK_X_RIGHT);
}

void MainUI::on_btnSlip_clicked()
{
    QToolButton* btn = qobject_cast<QToolButton*> (sender());
    
    if(btn->isChecked())
    {
        m_isOpenSlip = true;
    }
    else 
    {
        m_isOpenSlip = false;
    }
    
}

void MainUI::on_btnScreenshot_clicked()
{
    m_chartScreenshot();
}

void MainUI::on_btnCSV_clicked()
{
    
}

void MainUI::on_btnGit_clicked()
{
    QDesktopServices::openUrl(QUrl(QString("https://gitee.com/luckyxrq/qt-chart")));
}

void MainUI::on_btnRefreshSerial_clicked()
{
    m_refreshSerialList();
}

void MainUI::on_btnSerialOpenClose_clicked()
{
    /* 打开串口*/
    if(ui->btnSerialOpenClose->text() == QString("打开"))
    {
        QString portName = ui->cboxSerialList->currentText().split(":")[0];
        m_serial.setPortName(portName);
        
        if(m_serial.open(QIODevice::ReadWrite))
        {
            ui->cboxSerialList->setEnabled(false);
            ui->cboxBaud->setEnabled(false);
            ui->btnRefreshSerial->setEnabled(false);
            
            m_serial.setBaudRate(ui->cboxBaud->currentText().toInt());
            m_serial.setDataBits(QSerialPort::Data8);
            m_serial.setStopBits(QSerialPort::OneStop);
            m_serial.setParity(QSerialPort::NoParity);
            m_serial.setFlowControl(QSerialPort::NoFlowControl);
            
            /* 打开串口成功了才更改按钮文字*/
            ui->btnSerialOpenClose->setText(QString("关闭"));
            ui->btnSerialOpenClose->setIcon(QIcon(":/image/dot_green.png"));
        }
        else
        {
            QMessageBox::warning(nullptr, "提示", "\r\n串口无法打开，串口不存在或已被占用\r\n");
            return;
        }
    }
    
    /* 关闭串口*/
    else if(ui->btnSerialOpenClose->text() == QString("关闭")) 
    {
        ui->cboxSerialList->setEnabled(true);
        ui->cboxBaud->setEnabled(true);
        ui->btnRefreshSerial->setEnabled(true);
        
        /*串口对象清空*/
        m_serial.flush();
        m_serial.clear();
        m_serial.close();
        
        /*清空串口接收总BUF*/
        m_SerialRevBuf.clear();
        memset(analysisBuf,0,sizeof(analysisBuf));
        analysisIndex = 0;
        m_crcErrCnt = 0 ;
        m_crcPassCnt = 0 ;
        m_revPtCnt = 0 ;
        
        ui->btnSerialOpenClose->setText(QString("打开"));
        ui->btnSerialOpenClose->setIcon(QIcon(":/image/dot_red.png"));
    }
    else
    {
        /*理论不会进入此分支*/
        qDebug()<<"异常分支:on_btnSerialOpenClose_clicked";
    } 
}





